package cn.mrcode.fd.blog.controller;

import cn.mrcode.fd.blog.buutil.FilePathUtil;
import cn.mrcode.fd.blog.buutil.ImgPathInfo;
import cn.mrcode.fd.blog.common.BaseController;
import cn.mrcode.fd.blog.entity.BlogConfig;
import cn.mrcode.fd.blog.entity.User;
import cn.mrcode.fd.blog.entityCu.BlogConfigCu;
import cn.mrcode.fd.blog.entityCu.UserCu;
import cn.mrcode.fd.blog.services.IBlogConfigService;
import cn.mrcode.fd.blog.services.IUserService;
import cn.mrcode.fd.utils.io.FileUploadUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 用户信息设置。博客设置
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/1/12 20:51
 */
@Controller
@RequestMapping("/auth/user")
public class UserInfoController extends BaseController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IBlogConfigService iBlogConfigService;

    //个人资料设置
    @RequestMapping("/setprofile")
    public String setprofile(Model model) {
        UserCu user = getUser();
        model.addAttribute("user", user);
        return "/user/setprofile";
    }

    @RequestMapping("/setprofile_submit")
    public String setprofile(UserCu userCu, Model model) {
        UserCu user = getUser();
        String name = userCu.getName();
        String sign = userCu.getSign();
        if (StringUtils.isBlank(name)) {
            model.addAttribute("user", userCu);
            return "/user/setprofile";
        }
        if (StringUtils.isBlank(sign)) {
            model.addAttribute("user", userCu);
            return "/user/setprofile";
        }
        if (name.length() > 12) {
            model.addAttribute("user", userCu);
            return "/user/setprofile";
        }
        if (sign.length() > 40) {
            model.addAttribute("user", userCu);
            return "/user/setprofile";
        }

        iUserService.modProfile(user.getId(), name, sign);
        user.setName(name);
        user.setSign(sign);
        model.addAttribute("user", user);
        model.addAttribute("result_msg", "保存成功");
        return "/user/setprofile";
    }

    //个人头像设置
    @RequestMapping("/setavator")
    public String setavator(Model model) {
        UserCu user = getUser();
        model.addAttribute("user", user);
        return "/user/setavator";
    }

    @RequestMapping(value = "/setavator_submit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String setavator_submit(Model model, HttpServletRequest request) {
        UserCu user = getUser();
        model.addAttribute("user", user);

        JSONObject result = new JSONObject();
        User modUser = new User(); //需要更新的数据
        ImgPathInfo imgPathInfo = FilePathUtil.generImgPathInfo(FilePathUtil.IMG_HEAD_IMG, user.getAccount());
        boolean flag = FileUploadUtil.springUpImg(request, imgPathInfo.getAbsFolderPath(), imgPathInfo.getImgName(),true);
        if (!flag) {
            result.put("success", 0);
            result.put("message", "上传失败");
            return result.toJSONString();
        }

        modUser.setHeadImg(imgPathInfo.getRelDbImgPath());
        modUser.setId(user.getId());
        modUser.setModBy(user.getId());
        modUser.setModTime(new Date());
        iUserService.modByKeySelective(modUser);
        user.setHeadImg(modUser.getHeadImg());
        result.put("success", 1);
        result.put("message", "上传成功");
        return result.toJSONString();
    }

    /**
     * 博客秀设置
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/blogshow_submit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String blogshow_submit(Model model, HttpServletRequest request) {
        UserCu user = getUser();
        model.addAttribute("user", user);

        JSONObject result = new JSONObject();

        ImgPathInfo imgPathInfo = FilePathUtil.generImgPathInfo(FilePathUtil.IMG_BLOG_SHOW, user.getAccount());
        boolean flag = FileUploadUtil.springUpImg(request, imgPathInfo.getAbsFolderPath(), imgPathInfo.getImgName(),true);
        if (!flag) {
            result.put("success", 0);
            result.put("message", "上传失败");
            return result.toJSONString();
        }

        BlogConfig blogConfig = new BlogConfig();  //需要更新的数据
        blogConfig.setId(user.getBlogConfig().getId());
        blogConfig.setModBy(user.getId());
        blogConfig.setModTime(new Date());
        blogConfig.setBlogShow(imgPathInfo.getRelDbImgPath());
        iBlogConfigService.updateByPrimaryKeySelective(blogConfig);

        user.getBlogConfig().setBlogShow(imgPathInfo.getRelDbImgPath());
        result.put("success", 1);
        result.put("message", "上传成功");
        return result.toJSONString();
    }

    /** 设置背景 */
    @RequestMapping(value = "/sethubg", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String sethubg(String handerBg) {
        JSONObject result = new JSONObject();
        UserCu user = getUser();
        if (StringUtils.isNotBlank(handerBg)) {
            BlogConfigCu blogConfigCu = user.getBlogConfig();
            BlogConfig blogConfig = (BlogConfig) blogConfigCu;
            blogConfig.setHanderBg(handerBg);
            blogConfig.setModBy(user.getId());
            blogConfig.setModTime(new Date());
            iBlogConfigService.updateByPrimaryKeySelective(blogConfig);
            result.put("success", 1);
            result.put("message", "保存成功");
        } else {
            result.put("success", 0);
            result.put("message", "保存失败，请稍候重试");
        }
        return result.toJSONString();
    }
}
